package cn.stylefeng.guns.modular.business.frequency.ordermodifydetail.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.core.exception.ServiceException;
import cn.stylefeng.guns.core.factory.PageFactory;
import cn.stylefeng.guns.core.pojo.page.PageResult;
import cn.stylefeng.guns.modular.business.frequency.ordermodifydetail.entity.OrderModifyDetail;
import cn.stylefeng.guns.modular.business.frequency.ordermodifydetail.mapper.OrderModifyDetailMapper;
import cn.stylefeng.guns.modular.business.frequency.ordermodifydetail.model.param.OrderModifyDetailParam;
import cn.stylefeng.guns.modular.business.frequency.ordermodifydetail.service.OrderModifyDetailService;
import cn.stylefeng.guns.modular.business.frequency.ordermodifydetail.enums.OrderModifyDetailExceptionEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 订单修改记录明细表 服务实现类
 *
 * @author yujie
 * @date 2023/05/09 17:16
 */
@Service
public class OrderModifyDetailServiceImpl extends ServiceImpl<OrderModifyDetailMapper, OrderModifyDetail> implements OrderModifyDetailService {

    @Override
    public PageResult<OrderModifyDetail> page(OrderModifyDetailParam orderModifyDetailParam) {

        // 构造条件
        LambdaQueryWrapper<OrderModifyDetail> queryWrapper = new LambdaQueryWrapper<>();

        // 查询分页结果
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    public List<OrderModifyDetail> list(OrderModifyDetailParam orderModifyDetailParam) {

        // 构造条件
        LambdaQueryWrapper<OrderModifyDetail> queryWrapper = new LambdaQueryWrapper<>();

        return this.list(queryWrapper);
    }

    @Override
    public void edit(OrderModifyDetailParam orderModifyDetailParam) {

        // 根据id查询实体
        OrderModifyDetail orderModifyDetail = this.queryOrderModifyDetail(orderModifyDetailParam);

        // 请求参数转化为实体
        BeanUtil.copyProperties(orderModifyDetailParam, orderModifyDetail);

        this.updateById(orderModifyDetail);
    }

    @Override
    public OrderModifyDetail detail(OrderModifyDetailParam orderModifyDetailParam) {
        return this.queryOrderModifyDetail(orderModifyDetailParam);
    }



    /**
     * 获取订单修改记录明细表
     *
     * @author yujie
     * @since 2023/05/09 17:16
     */
    private OrderModifyDetail queryOrderModifyDetail(OrderModifyDetailParam orderModifyDetailParam) {
        OrderModifyDetail orderModifyDetail = this.getById(orderModifyDetailParam.getId());
        if (ObjectUtil.isEmpty(orderModifyDetail)) {
            throw new ServiceException(OrderModifyDetailExceptionEnum.NOT_EXISTED);
        }
        return orderModifyDetail;
    }
}